import React, { Component } from 'react'
import { Table } from 'antd'
import PropTypes from 'prop-types'

import HTTP from "../../utils/HTTP";

export default class extends Component {
    static propTypes = {
        columns: PropTypes.array,
        url: PropTypes.string, //请求 url
        searchParam: PropTypes.object, //搜索提交
        onRowDoubleClick: PropTypes.func
    }
    constructor(props) {
        super(props)
        this.state = {
            innerData: [],
            pagination: false,
            loading: false
        }
    }
    componentDidMount() {
        this.loadData()
    }

    render = () => {
        const {onRowDoubleClick} = this.props
        return (
            <Table
                columns={this.props.columns}
                rowKey={record => record.id}
                dataSource={this.state.innerData}
                pagination={false}
                // loading={this.state.loading}
                // onChange={this.handleTableChange}
                // onRowDoubleClick={onRowDoubleClick}
            />
        )
    }

    handleTableChange = (pagination, filters, sorter) => {
        const pager = { ...this.state.pagination };
        pager.current = pagination.current;
        this.setState({
            pagination: pager
        }, () => {
            this.loadData()
        })
    }

    loadData = () => {
        this.setState({loading: true})
        const pager = {
            page: (this.state.pagination.current || 1) - 1,
        }
        const params = Object.assign({}, this.props.searchParam, pager)
        HTTP.post(this.props.url, params,  () => {
            this.setState({ loading: false })
        }, (resp) => {
            this.setState({
                innerData: resp
            })
        })
    }
}